﻿package eu.liquify.ui {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.ui.Keyboard;
	import no.doomsday.console.ConsoleUtil;
	public class TextBox extends UIItem {
		private var defaultText:String = "";
		//private var labelWidth:Number = NaN;
		public var labelWidth:Number = 40;
		//public var labelColor:int = 0x000000;
		public var spacing:Number = 3;
		
		protected var content:Sprite = new Sprite();
		protected var labelTxt:Label = new Label();
		protected var txt:TextField = new TextField();
		
		public function TextBox(options:Object = null) {
			width = 100;
			height = 18;
			labelTxt.padding = 0;
			
			applyOptions(options);
			
			addChild(content);
			
			labelTxt.label = label;
			content.addChild(labelTxt);
			
			txt.defaultTextFormat = new TextFormat("Tahoma", 9, null, true, null, null, null, null, TextFormatAlign.LEFT);
			txt.text = defaultText;
			txt.height = 15;
			txt.type = TextFieldType.INPUT;
			txt.border = true;
			txt.borderColor = 0xDBDBDB;
			txt.background = true;
			txt.backgroundColor = 0xEFEFEF;
			txt.addEventListener(FocusEvent.FOCUS_OUT, focusOut);
			txt.addEventListener(KeyboardEvent.KEY_DOWN, keyDown);
			txt.tabIndex = nextTabIndex++;
			content.addChild(txt);
			
			//resizeLabel(labelWidth);
			//updateSize();
			
			resize();
			
		}
		
		override public function resize():void {
			super.resize();
			if (fixedSize) {
				resizeLabel(width/2-spacing);
			} else {
				resizeLabel(labelWidth);
				//resizeLabel(20);
			}
		}
		
		override public function set label(value:String):void {
			super.label = value;
			labelTxt.label = label;
		}
		
		public function get text():String {
			return txt.text;
		}
		public function set text(v:String):void {
			txt.text = v;
			updateSize();
		}	
		public function get backgroundColor():uint {
			return txt.backgroundColor;
		}
		public function set backgroundColor(v:uint):void {
			txt.backgroundColor = v;
		}
		
		private function resizeLabel(w:Number):void {
			labelWidth = w;
			if (isNaN(labelWidth)) {
				//labelTxt.autoSize = TextFieldAutoSize.LEFT;
				//labelWidth = labelTxt.width;
				labelTxt.fixedSize = false;
				labelTxt.resize();
				labelWidth = labelTxt.width;
			} else {
				labelTxt.fixedSize = true;
				labelTxt.width = labelWidth;
				//labelTxt.height = txt.height;
				//labelTxt.stretch = true;
				labelTxt.resize();
				//Utils.resizeTextField(labelTxt, label, labelWidth);
				//labelTxt.x = labelWidth-labelTxt.width;
			}
			//txt.x = labelTxt.x+labelTxt.width+spacing;
			txt.x = labelTxt.x+labelWidth+spacing;
			updateSize();
		}
		
		private function updateSize():void {
			txt.scrollH = 0;
			if (fixedSize) {
				txt.width = Math.max(0, width-txt.x);
				content.y = height/2-content.height/2-1;
			} else {
				width = txt.x+txt.width;
				height = content.height;
			}
			//height = content.height;
		}
		
		private function keyDown(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.ENTER) updateChange();
		}
		private function focusOut(e:FocusEvent):void {
			updateChange();
		}
		protected function updateChange():void {
			dispatchEvent(new Event(Event.CHANGE));
		}
	}
}